package com.song.cmsservice.controller;



import com.song.cmsservice.entity.UcenterMember;
import com.song.cmsservice.entity.vo.RegisterVo;
import com.song.cmsservice.service.UcenterMemberService;
import com.song.commonutils.JwtUtils;
import com.song.commonutils.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-09
 */
@RestController
@RequestMapping("/cmsservice/member")
@Api(description = "前台登陆注册管理")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @ApiOperation("登陆")
    @PostMapping("login")
    public ResultObj loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        Object token = ucenterMemberService.login(member);
        return ResultObj.ok().data("token",token);
    }

    /**
     * 注册
     * @param registerVo
     * @return
     */
    @ApiOperation("注册")
    @PostMapping("register")
    public ResultObj registerUser(@RequestBody RegisterVo registerVo) {
        ResultObj register = ucenterMemberService.register(registerVo);
        return ResultObj.ok().data("data",register);
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public ResultObj getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        System.out.println(member);
        return ResultObj.ok().data("userInfo",member);
    }

    /**
     * 查询某一天的注册人数
     * @param day
     * @return
     */
    @ApiOperation("查询某一天的注册人数")
    @GetMapping("/countRegister/{day}")
    public ResultObj countRegister(@PathVariable String day){
        Integer count=ucenterMemberService.countRegister(day);
        return ResultObj.ok().data("count",count);
    }
}

